package com.dony.web.controller.store;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dony.common.annotation.Log;
import com.dony.common.core.controller.BaseController;
import com.dony.common.core.domain.AjaxResult;
import com.dony.common.core.page.TableDataInfo;
import com.dony.common.enums.BusinessType;
import com.dony.common.utils.SecurityUtils;
import com.dony.shop.domain.Supplier;
import com.dony.shop.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 供应商Controller
 *
 * @author sundw
 * @date 2023-08-30
 */
@RestController
@RequestMapping("/store/supplier")
public class SupplierController extends BaseController {
    @Autowired
    private ISupplierService supplierService;

    /**
     * 查询列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Supplier supplier) {
        startPage();
        List<Supplier> list = supplierService.listByParam(supplier);
        return getDataTable(list);
    }

    /**
     * 查询当前租户的供应商列表
     */
    @GetMapping("/all")
    public AjaxResult listAllSupplier() {
        return AjaxResult.success(supplierService.listAllSupplier());
    }


    /**
     * 获取详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(supplierService.getById(id));
    }

    /**
     * 新增
     */
    @Log(title = "供应商", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Supplier supplier) {
        //保存租户信息
        supplier.setTenantId(SecurityUtils.getDeptId());
        return toAjax(supplierService.save(supplier));
    }

    /**
     * 修改供应商
     */
    @Log(title = "供应商", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Supplier supplier) {
        return toAjax(supplierService.updateById(supplier));
    }

    /**
     * 删除供应商
     */
    @Log(title = "供应商", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(supplierService.removeBatchByIds(Arrays.asList(ids)));
    }
}
